<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/17 10:58
 * Desc: 会员导游
 */
class Controller_Pc_Api_V2_Member_Guide extends Controller_Pc_Api_V2_Base
{
    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Member_Guide
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Member_Guide
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('member_guide', $this->request_params);

        // 服务
        $this->service = new Service_Api_Member_Guide();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }


    // 会员是否导游
    public function action_is_guide()
    {
        try {
            $this->validation->is_guide()->check();

            $this->result['data'] = $this->service->is_guide($this->mid);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 导游认证状态
    public function action_certify_status()
    {
        try {
            $this->validation->certify_status()->check();

            $this->result['data'] = $this->service->certify_status($this->mid);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 提交导游认证
    public function action_certify()
    {
        try {
            $this->validation->certify()->check();

            $this->result['data'] = $this->service->certify($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 获取导游邀请码(没有找到生成导游二维码的地方，前端已关闭入口)
    public function action_get_qrcode()
    {
        try {
            $this->validation->get_qrcode()->check();

            $this->result['data'] = $this->service->get_qrcode($this->mid);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }


    // 带团记录
    public function action_record()
    {
        try {
            $this->validation->record()->check();

            $this->result['data'] = $this->service->record($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 扫码接单
    public function action_scan_code()
    {
        try {
            $this->validation->scan_code()->check();

            $this->result['data'] = $this->service->scan_code($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 开始接单
    public function action_start_service()
    {
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->start_service()->check();

            $this->result['data'] = $this->service->start_service($this->mid, $this->request_params);

            $db->commit();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());

            $db->rollback();
        }
    }

    // 结束接单
    public function action_end_service()
    {
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->end_service()->check();

            $this->result['data'] = $this->service->end_service($this->mid, $this->request_params);
            $db->commit();
        } catch (Exception $ex) {
            $this->result = array('status' => false, 'msg' => $ex->getMessage());
            $db->rollback();
        }
    }
}
